//----------------------------头文件依赖----------------------------//
#include "LCD_TL040WVS03.h"
#include "dev_systick.h"

/* TFT-LCD 模组名 : TL040WVS03 (冠显光电), 屏内 COG 芯片型号 : ST7701S */

//----------------------------本地宏----------------------------//
#if (SWM34S_LCM_PCBV == SWM34SRE_PIN64_A001)// SWM34SRET6_Pin64

#define LCD_GPIO_CS 			GPIOD
#define LCD_PIN_CS 				PIN9

#define LCD_GPIO_SCK 			GPION
#define LCD_PIN_SCK 			PIN5

#define LCD_GPIO_SDA 			GPION
#define LCD_PIN_SDA 			PIN4

#else // SWM34SVET6_Pin100

#define LCD_GPIO_CS 			GPIOB
#define LCD_PIN_CS 				PIN0

#define LCD_GPIO_SCK 			GPION
#define LCD_PIN_SCK 			PIN5

#define LCD_GPIO_SDA 			GPION
#define LCD_PIN_SDA 			PIN4

#endif

//操作宏
#if 	0 //库函数

#define PIN_HIGH_RST() 		GPIO_AtomicSetBit(LCD_GPIO_RST, LCD_PIN_RST) //GPIO_SetBit(LCD_GPIO_RST, LCD_PIN_RST)
#define PIN_LOW_RST() 		GPIO_AtomicClrBit(LCD_GPIO_RST, LCD_PIN_RST) //GPIO_ClrBit(LCD_GPIO_RST, LCD_PIN_RST)

#define PIN_HIGH_CS() 		GPIO_AtomicSetBit(LCD_GPIO_CS, LCD_PIN_CS) //GPIO_SetBit(LCD_GPIO_CS, LCD_PIN_CS)
#define PIN_LOW_CS() 		GPIO_AtomicClrBit(LCD_GPIO_CS, LCD_PIN_CS) //GPIO_ClrBit(LCD_GPIO_CS, LCD_PIN_CS)

#define PIN_HIGH_SCK() 		GPIO_AtomicSetBit(LCD_GPIO_SCK, LCD_PIN_SCK) //GPIO_SetBit(LCD_GPIO_SCK, LCD_PIN_SCK)
#define PIN_LOW_SCK() 		GPIO_AtomicClrBit(LCD_GPIO_SCK, LCD_PIN_SCK) //GPIO_ClrBit(LCD_GPIO_SCK, LCD_PIN_SCK)

#define PIN_HIGH_SDA() 		GPIO_AtomicSetBit(LCD_GPIO_SDA, LCD_PIN_SDA) //GPIO_SetBit(LCD_GPIO_SDA, LCD_PIN_SDA)
#define PIN_LOW_SDA() 		GPIO_AtomicClrBit(LCD_GPIO_SDA, LCD_PIN_SDA) //GPIO_ClrBit(LCD_GPIO_SDA, LCD_PIN_SDA)

#else //寄存器

#define PIN_HIGH_RST() 		(*(&LCD_GPIO_RST->DATAPIN0 + LCD_PIN_RST) = 1) //LCD_GPIO_RST->ODR |= (0x01 << LCD_PIN_RST)
#define PIN_LOW_RST() 		(*(&LCD_GPIO_RST->DATAPIN0 + LCD_PIN_RST) = 0) //LCD_GPIO_RST->ODR &= ~(0x01 << LCD_PIN_RST)

#define PIN_HIGH_CS() 		(*(&LCD_GPIO_CS->DATAPIN0 + LCD_PIN_CS) = 1) //LCD_GPIO_CS->ODR |= (0x01 << LCD_PIN_CS)
#define PIN_LOW_CS() 		(*(&LCD_GPIO_CS->DATAPIN0 + LCD_PIN_CS) = 0) //LCD_GPIO_CS->ODR &= ~(0x01 << LCD_PIN_CS)

#define PIN_HIGH_SCK() 		(*(&LCD_GPIO_SCK->DATAPIN0 + LCD_PIN_SCK) = 1) //LCD_GPIO_SCK->ODR |= (0x01 << LCD_PIN_SCK)
#define PIN_LOW_SCK() 		(*(&LCD_GPIO_SCK->DATAPIN0 + LCD_PIN_SCK) = 0) //LCD_GPIO_SCK->ODR &= ~(0x01 << LCD_PIN_SCK)

#define PIN_HIGH_SDA() 		(*(&LCD_GPIO_SDA->DATAPIN0 + LCD_PIN_SDA) = 1) //LCD_GPIO_SDA->ODR |= (0x01 << LCD_PIN_SDA)
#define PIN_LOW_SDA() 		(*(&LCD_GPIO_SDA->DATAPIN0 + LCD_PIN_SDA) = 0) //LCD_GPIO_SDA->ODR &= ~(0x01 << LCD_PIN_SDA)

#endif

//----------------------------本地函数定义----------------------------//
static inline void spi_delay_time(void)
{
	for (int us = 0; us < 100; us++)
		__NOP();
}

static void spi_send_data(unsigned char i)
{
	for (unsigned char n = 0; n < 8; n++)
	{
		if (i & 0x80)
		{
			PIN_HIGH_SDA();
		}
		else
		{
			PIN_LOW_SDA();
		}
		i <<= 1;
		spi_delay_time();
		PIN_LOW_SCK();
		spi_delay_time();
		PIN_HIGH_SCK();
		spi_delay_time();
	}
}

static void spi_write_cmd(unsigned char i)
{
	PIN_LOW_CS();
	spi_delay_time();
	PIN_LOW_SDA();
	spi_delay_time();
	PIN_LOW_SCK();
	spi_delay_time();

	PIN_HIGH_SCK();
	spi_delay_time();
	spi_send_data(i);
	spi_delay_time();
	PIN_HIGH_CS();
	spi_delay_time();
}

static void spi_write_data(unsigned char i)
{
	PIN_LOW_CS();
	spi_delay_time();
	PIN_HIGH_SDA();
	spi_delay_time();
	PIN_LOW_SCK();
	spi_delay_time();

	PIN_HIGH_SCK();
	spi_delay_time();
	spi_send_data(i);
	spi_delay_time();
	PIN_HIGH_CS();
	spi_delay_time();
}

static void spi_port_init(void)
{
	GPIO_Init(LCD_GPIO_CS, LCD_PIN_CS, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_SCK, LCD_PIN_SCK, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_SDA, LCD_PIN_SDA, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_RST, LCD_PIN_RST, 1, 1, 0, 0);
	PIN_HIGH_CS();
	PIN_HIGH_SCK();
	PIN_HIGH_SDA();

	PIN_HIGH_RST(); //复位
	spi_delay_time();//swm_delay_ms(1);
	PIN_LOW_RST();
	swm_delay_ms(1);
	PIN_HIGH_RST();
}

//----------------------------对外函数定义----------------------------//
void lcd_spi_init_TL040WVS03(void)
{
	spi_port_init();

	PIN_LOW_CS();
	spi_delay_time();
	PIN_HIGH_SDA();
	swm_delay_ms(8);

	PIN_LOW_SDA();
	swm_delay_ms(50);
	PIN_HIGH_SDA();
	swm_delay_ms(50);

	spi_write_cmd(0x11); //BOE3.97IPS
	swm_delay_ms(150);

#if 1 //缩进便于观看
	spi_write_cmd(0xFF);
	spi_write_data(0x77);
	spi_write_data(0x01);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x10);
	spi_write_cmd(0xC0);
	spi_write_data(0x3B);
	spi_write_data(0x00);
	spi_write_cmd(0xC1);
	spi_write_data(0x0D);
	spi_write_data(0x02);
	spi_write_cmd(0xC2);
	spi_write_data(0x31);
	spi_write_data(0x05);
	spi_write_cmd(0xCd);
	spi_write_data(0x08);

	spi_write_cmd(0xB0);
	spi_write_data(0x00);
	spi_write_data(0x11);
	spi_write_data(0x18);
	spi_write_data(0x0E);
	spi_write_data(0x11);
	spi_write_data(0x06);
	spi_write_data(0x07);
	spi_write_data(0x08);
	spi_write_data(0x07);
	spi_write_data(0x22);
	spi_write_data(0x04);
	spi_write_data(0x12);
	spi_write_data(0x0F);
	spi_write_data(0xAA);
	spi_write_data(0x31);
	spi_write_data(0x18);
	spi_write_cmd(0xB1);
	spi_write_data(0x00);
	spi_write_data(0x11);
	spi_write_data(0x19);
	spi_write_data(0x0E);
	spi_write_data(0x12);
	spi_write_data(0x07);
	spi_write_data(0x08);
	spi_write_data(0x08);
	spi_write_data(0x08);
	spi_write_data(0x22);
	spi_write_data(0x04);
	spi_write_data(0x11);
	spi_write_data(0x11);
	spi_write_data(0xA9);
	spi_write_data(0x32);
	spi_write_data(0x18);

	spi_write_cmd(0xFF);
	spi_write_data(0x77);
	spi_write_data(0x01);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x11);

	spi_write_cmd(0xB0);
	spi_write_data(0x60);

	spi_write_cmd(0xB1);
	spi_write_data(0x32);

	spi_write_cmd(0xB2);
	spi_write_data(0x07);

	spi_write_cmd(0xB3);
	spi_write_data(0x80);

	spi_write_cmd(0xB5);
	spi_write_data(0x49);

	spi_write_cmd(0xB7);
	spi_write_data(0x85);

	spi_write_cmd(0xB8);
	spi_write_data(0x21);

	spi_write_cmd(0xC1);
	spi_write_data(0x78);

	spi_write_cmd(0xC2);
	spi_write_data(0x78);

	spi_write_cmd(0xE0);
	spi_write_data(0x00);
	spi_write_data(0x1B);
	spi_write_data(0x02);

	spi_write_cmd(0xE1);
	spi_write_data(0x08);
	spi_write_data(0xA0);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x07);
	spi_write_data(0xA0);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x44);
	spi_write_data(0x44);

	spi_write_cmd(0xE2);
	spi_write_data(0x11);
	spi_write_data(0x11);
	spi_write_data(0x44);
	spi_write_data(0x44);
	spi_write_data(0xED);
	spi_write_data(0xA0);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0xEC);
	spi_write_data(0xA0);
	spi_write_data(0x00);
	spi_write_data(0x00);

	spi_write_cmd(0xE3);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x11);
	spi_write_data(0x11);

	spi_write_cmd(0xE4);
	spi_write_data(0x44);
	spi_write_data(0x44);

	spi_write_cmd(0xE5);
	spi_write_data(0x0A);
	spi_write_data(0xE9);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x0C);
	spi_write_data(0xEB);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x0E);
	spi_write_data(0xED);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x10);
	spi_write_data(0xEF);
	spi_write_data(0xD8);
	spi_write_data(0xA0);

	spi_write_cmd(0xE6);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x11);
	spi_write_data(0x11);

	spi_write_cmd(0xE7);
	spi_write_data(0x44);
	spi_write_data(0x44);

	spi_write_cmd(0xE8);
	spi_write_data(0x09);
	spi_write_data(0xE8);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x0B);
	spi_write_data(0xEA);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x0D);
	spi_write_data(0xEC);
	spi_write_data(0xD8);
	spi_write_data(0xA0);
	spi_write_data(0x0F);
	spi_write_data(0xEE);
	spi_write_data(0xD8);
	spi_write_data(0xA0);

	spi_write_cmd(0xEB);
	spi_write_data(0x02);
	spi_write_data(0x00);
	spi_write_data(0xE4);
	spi_write_data(0xE4);
	spi_write_data(0x88);
	spi_write_data(0x00);
	spi_write_data(0x40);

	spi_write_cmd(0xEC);
	spi_write_data(0x3C);
	spi_write_data(0x00);

	spi_write_cmd(0xED);
	spi_write_data(0xAB);
	spi_write_data(0x89);
	spi_write_data(0x76);
	spi_write_data(0x54);
	spi_write_data(0x02);
	spi_write_data(0xFF);
	spi_write_data(0xFF);
	spi_write_data(0xFF);
	spi_write_data(0xFF);
	spi_write_data(0xFF);
	spi_write_data(0xFF);
	spi_write_data(0x20);
	spi_write_data(0x45);
	spi_write_data(0x67);
	spi_write_data(0x98);
	spi_write_data(0xBA);

	spi_write_cmd(0x36);
	spi_write_data(0x00);
	spi_write_cmd(0xFF);
	spi_write_data(0x77);
	spi_write_data(0x01);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x13);

	spi_write_cmd(0xE5);
	spi_write_data(0xE4);

	spi_write_cmd(0xFF);
	spi_write_data(0x77);
	spi_write_data(0x01);
	spi_write_data(0x00);
	spi_write_data(0x00);
	spi_write_data(0x00);

	spi_write_cmd(0x3A);
	spi_write_data(0x60);
	spi_write_cmd(0x21);

#endif
	swm_delay_ms(1);

	spi_write_cmd(0x11);
	spi_write_cmd(0x29);

	swm_delay_ms(10);
}
